using System;
using System.Collections.Generic;
using System.Text;

namespace SavedVariablesParser.Line {
    /// <summary>
    /// Represents a line in a World of Warcraft saved variables file that matches the end of index format.
    /// </summary>
    public class EndIndex : LineBase{
        /// <summary>
        /// 	<para>Initializes an instance of the <see cref="EndIndex"/> class.</para>
        /// </summary>
        /// <param name="text">The text of the line.</param>
        /// <param name="type">Specifies the type of line.</param>
        /// <param name="lineNumber">Specifies the line number within the original file where this line can be found.</param>
        /// <exception cref="ArgumentNullException">
        /// 	<para>The argument <paramref name="text"/> is <langword name="null"/>.</para>
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// 	<para>The argument <paramref name="text"/> is out of range.</para>
        /// 	<para>-or-</para>
        /// 	<para>The argument <paramref name="lineNumber"/> is out of range.</para>
        /// </exception>
        public EndIndex(string text, LineType type, int lineNumber) {
            if (text == null) {
                throw new ArgumentNullException("text");
            }
            if (text.Length == 0) {
                throw new ArgumentOutOfRangeException("text", "The value of 'text' cannot be a zero length string.");
            }
            if (lineNumber <= 0) {
                throw new ArgumentOutOfRangeException("lineNumber", "The value of 'lineNumber' must be greater than zero.");
            }

            base.text = text;
            base.type = type;
            base.lineNumber = lineNumber;
        }

        /// <summary>
        /// Renders the line and returns a string representing the rendered value.
        /// </summary>
        /// <returns>The text line rendered in a predefined format.</returns>
        public override string Render() {
            StringBuilder builder = new StringBuilder();
            this.GenerateEndIndexNode(builder);
            return builder.ToString();
        }

        /// <summary>
        /// Renders the line to the supplied <see cref="StringBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to render the string to.</param>
        public override void Render(StringBuilder builder) {
            this.GenerateEndIndexNode(builder);
        }

        /// <summary>
        /// Generates the end index node tag.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to render the tag to.</param>
        private void GenerateEndIndexNode(StringBuilder builder) {
            //</element>
            builder.Append("</element>");
            builder.Append(Environment.NewLine);
        }
    }
}
